package top.whysu.gps.controller.system;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.whysu.gps.dto.generate.RequestDTO;
import top.whysu.gps.dto.generate.ResponseDTO;
import top.whysu.gps.dto.page.PagenationDataDTO;
import top.whysu.gps.dto.system.AccountDTO;
import top.whysu.gps.dto.system.AccountQueryDTO;
import top.whysu.gps.service.system.AccountService;
import top.whysu.gps.service.system.LoginService;
import top.whysu.gps.utils.ResponseUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 账号管理
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Resource
    private AccountService accountService;
    @Resource
    private LoginService loginService;

    //----------------界面----------------

    /**
     * 账号管理界面
     */
    @GetMapping("/accountManagement")
    public String accountManager() {
        return "account/accountManagement";
    }

    // ---------------接口---------------------
    /**
     * 账户管理-添加账号、编辑账号（根据id是否有值来区分）
     *
     * @return
     */
    @PostMapping("/addOrModify")
    @ResponseBody
    public ResponseDTO add(@RequestBody final RequestDTO<AccountDTO> request) {
        AccountDTO accountDTO = request.getBody();
        if (accountDTO.getId() != null) {
            // 编辑
            accountService.updateAccount(request.getBody());
        } else {
            // 新增
            accountService.insertAccount(request.getBody());
        }
        return ResponseUtil.getSuccessResponse(request.getHeader());
    }

    /**
     * 账户管理-查看账号
     */
    @PostMapping("/get")
    @ResponseBody
    public ResponseDTO<AccountDTO> get(@RequestBody final RequestDTO<BigDecimal> request) {
        AccountDTO accountDTO = accountService.selectAccount(request.getBody());
        return ResponseUtil.getSuccessResponse(request.getHeader(), accountDTO);
    }

    /**
     * 账户管理-列表查询
     *
     * @return
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<AccountDTO>> getPage(@RequestBody AccountQueryDTO accountQueryDTO) {
        PagenationDataDTO<AccountDTO> accountPage = accountService.selectPageAccount(accountQueryDTO);
        return ResponseUtil.getSuccessResponse(accountPage);
    }

    /**
     * 账户管理-重置账号密码
     *
     * @return
     */
    @PostMapping("/resetPwd")
    @ResponseBody
    public ResponseDTO<String> reset(@RequestBody final RequestDTO<BigDecimal> request) {
        accountService.wholeResetAccount(request.getBody());
        return ResponseUtil.getSuccessResponse(request.getHeader());
    }

    /**
     * 账户管理-删除账号
     *
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResponseDTO<String> delete(@RequestBody final RequestDTO<BigDecimal> request) {
        accountService.deleteAccount(request.getBody());
        return ResponseUtil.getSuccessResponse(request.getHeader());
    }

    /**
     * 是否首次登录
     *
     * @return 应答数据传输对象
     */
    @RequestMapping("/firstLogin")
    @ResponseBody
    public ResponseDTO<Boolean> firstLogin(@RequestBody final RequestDTO request) {
        Boolean result = loginService.isFirstLogin();
        return ResponseUtil.getSuccessResponse(request.getHeader(), result);
    }

}
